﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Customer;
using KerriganDataContract.Person;
using KerriganDataContract.Agency;

namespace KerriganService.DAC
{
    public static class CustomerDAC
    {
        public static CustomerDC GetCustomer(int id)
        {
            vwGetCustomer entity = new vwGetCustomer();
            CustomerDC bo = new CustomerDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    bo = CopyEntityToBo( db.TCustomers.FirstOrDefault(f => f.IdCustomer == id && f.TAgencyEmployee != null));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static List<CustomerDC> GetAllCustomer()
        {
            List<CustomerDC> list = new List<CustomerDC>();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    list = CopyListEntitiesToBo((from customer in db.TCustomers
                                           where customer.TAgencyEmployee != null && customer.TPerson != null
                                           select customer).ToList());
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return list;
        }

        public static List<CustomerByInstructorByDate_Result> GetAllCustomerByInstructorToday(int idInstructor)
        {
            List<CustomerByInstructorByDate_Result> list = new List<CustomerByInstructorByDate_Result>();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    list = db.CustomerByInstructorByDate(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), idInstructor).ToList();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return list;
        }

        public static Boolean AddCustomer(CustomerDC dc)
        {
            TCustomer entity = new TCustomer();
            Boolean result = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = CustomerDAC.CopyBoToEntity(dc);
                    db.TCustomers.AddObject(entity);
                    db.SaveChanges();
                    result = true;

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return result;
        }

        public static Boolean AddCustomer(string email, AgencyEmployeeDC employee)
        {
            Boolean result = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    TCustomer customer = new TCustomer();
                    customer = db.TCustomers.FirstOrDefault(f => f.TPerson.Email == email);
                    if (customer != null)
                    {
                        customer.IdCommercialFK = employee.IdAgencyEmployee;
                        db.SaveChanges();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);

                }
            }
            return result;
        }

        public static CustomerInformationDC getCustomerInformation(int customerID)
        {
            CustomerInformationDC bo = new CustomerInformationDC();
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    bo.Customer = CustomerDAC.GetCustomer(customerID);
                    bo.Sessions = SessionCoursesDAC.GetSessionsByCustomer(customerID);
                    bo.Codes = CodeDAC.GetCodeByUser(customerID);
                    bo.Factures = FactureDAC.GetCustomerFacture(customerID);
                    bo.Exams = ExamDAC.GetCustomerExams(customerID);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static int Deletecustomer(int customerID)
        {
            int id = 0;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    id = customerID;
                    TCustomer entity = new TCustomer();
                    entity = db.TCustomers.FirstOrDefault(f => f.IdCustomer == id);
                    db.TCustomers.DeleteObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return id;
        }

        public static CustomerDC CopyEntityToBo(TCustomer entity)
        {
            CustomerDC bo = new CustomerDC();

            try
            {
                bo.IdCustomer = entity.IdCustomer;
                bo.bsr = (Boolean)entity.bsr;
                bo.permisa = (Boolean)entity.permisa;
                bo.permisb = (Boolean)entity.permisb;
                bo.dateCode = entity.dateCode;
                bo.Person = new PersonDC();
                bo.Person.Email = entity.TPerson.Email;
                bo.Person.FirstName = entity.TPerson.FirstName;
                bo.Person.LastName = entity.TPerson.LastName;
                bo.Person.NickName = entity.TPerson.NickName;
                bo.Person.Password = entity.TPerson.Password;
                bo.Person.Adress = entity.TPerson.Adress;
                bo.Person.City = entity.TPerson.City;
                bo.Person.PostCode = entity.TPerson.PostCode;
                bo.Person.Birthday = entity.TPerson.Birthday;
                bo.Person.PhoneNumber = entity.TPerson.PhoneNumber;
                bo.Commercial = new AgencyEmployeeDC();
                bo.Commercial = AgencyEmployeeDAC.CopyEntityToBo(entity.TAgencyEmployee);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }


        public static List<CustomerDC> CopyListEntitiesToBo(List<TCustomer> entities)
        {
            List<CustomerDC> bo = new List<CustomerDC>();

            try
            {
                foreach (TCustomer entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }


        public static TCustomer CopyBoToEntity(CustomerDC bo)
        {
            TCustomer entity = new TCustomer();

            try
            {
                if (bo.Commercial != null)
                {
                    entity.IdCommercialFK = bo.Commercial.IdAgencyEmployee;
                }
                entity.TPerson = new TPerson();
                entity.TPerson = PersonDAC.CopyBoToEntity(bo.Person);
                entity.IdCustomer = bo.IdCustomer;
                entity.bsr = bo.bsr;
                entity.permisa = bo.permisa;
                entity.permisb = bo.permisb;
                entity.dateCode = bo.dateCode;
                entity.TPerson.IdPersonType = 4;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }
    }
}
